<?php

class cart_model extends CI_Model {

    public function __construct() {
        parent::__construct();
    }

    public function addCart($session_id, $prd_id, $price,$member_id, $qty) {
        log_message('debug', get_class($this).' // $session_id ::: '.$session_id);
        log_message('debug', get_class($this).' // $prd_id ::: '.$prd_id);
        log_message('debug', get_class($this).' // $price ::: '.$price);
        log_message('debug', get_class($this).' // $member_id ::: '.$member_id);
        log_message('debug', get_class($this).' // $qty ::: '.$qty);

        
        if($this->haveExistingCart($session_id, $prd_id)){
            log_message('debug', get_class($this).' // Have Existing Cart Update Plus Qty.');
            
            $this->updatePlusExistingCart($session_id, $prd_id, $qty);
        } else {
            log_message('debug', get_class($this).' // Not Existing Cart Insert New Item.');
            
            $this->insertNewItemToCart($session_id, $prd_id, $price, $member_id, $qty);
        }

    }

    public function getCart($session){
        log_message('debug', get_class($this).'// Load Cart.');
        
        $query = $this->db->query("
            select * 
            from gs_cart gc
            inner join gs_product gs on gc.PRD_ID = gs.PRD_ID
            where gc.session_id = '".$session."'
            and status_id = ".CART_STAT_CART."
            order by gc.PRD_ID
        ");
        
        return $query;
    }
    
    private function haveExistingCart($session, $prd_id){
        $query = $this->db->query("
            select session_id from gs_cart 
            where session_id = '".$session."'
            and prd_id = ".$prd_id."
            and status_id = ".CART_STAT_CART."
        ");
        $row = $query->row();
        
        if($row){
            return true;
        } else {
            return false;
        }
    }
    
    private function insertNewItemToCart($session_id, $prd_id, $price,$member_id, $qty){        
        log_message('debug', get_class($this).' Insert To Cart.');
        $data = array(
            'SESSION_ID' => $session_id,
            'PRD_ID' => $prd_id,
            'WEB_MEMBER_ID' => $member_id,
            'QTY' => $qty,
            'UNIT_PRICE' => $price,
            'CREATED_DATETIME' => date('Y-m-d H:i:s'),
            'STATUS_ID' => CART_STAT_CART
        );

        $this->db->insert('gs_cart', $data);
    }
    
    private function updatePlusExistingCart($session, $prd_id, $qty){
        log_message('debug', get_class($this).' Update Cart.');
        $query = $this->db->query("
            select QTY from gs_cart 
            where session_id = '".$session."'
            and prd_id = ".$prd_id."
        ");
        $qtyDb = $query->row()->QTY;
        
        $data = array(
            'QTY' => $qty + $qtyDb
        );
        
        $this->db->where('SESSION_ID', $session);
        $this->db->where('PRD_ID', $prd_id);
        
        $this->db->update('gs_cart', $data); 
    }
    
    public function deleteCart($session, $prd_id){
        $this->db->where('SESSION_ID', $session);
        $this->db->where('PRD_ID', $prd_id);
        
        $this->db->delete('gs_cart', $data); 
    }
    
    public function getCartCount(){
        $query = $this->db->query("
            select count(*) as NUM
            from gs_cart where session_id = '".session_id()."'
        ");

        $row = $query->row();

        return $row->NUM;
    }
    
    public function updateAllPrdCart($MAP){
        foreach ($MAP as $key => $value) {
            $data = array(
                'QTY' => $value 
            );
            
            $this->db->where('SESSION_ID', session_id());
            $this->db->where('PRD_ID', $key);

            $this->db->update('gs_cart', $data);
        }
    }
}

?>
